package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.competent.OnMsgItemClickListener;
import gl.android.biyiqing.event.ReadMsg;
import gl.common.UserManger;

import org.json.JSONException;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 消息盒子
 * 
 * @author geliang
 * 
 */
public class MessageListFragment extends BaseListFragmet implements
		OnClickListener {

	protected final String ERR = "发送消息失败";
	protected final String OK = "发送成功";
	private TextView empty;
	private ListView list;
	protected String NOTFOUND = "没有消息";
	private Drawable drawable;
	private EditText et_msg;
	private View btn_send;
	private RadioGroup mRadioGroup;
	private ViewPager pager;

	// private PullToRefreshView pullToRefreshView1;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		et_msg = (EditText) findViewById(R.id.et_msg);
//		btn_send = findViewById(R.id.btn_send);
		// pullToRefreshView1 =
		// (PullToRefreshView)findViewById(R.id.pullToRefreshView1);
		// pullToRefreshView1.onHeaderRefreshComplete();
//		et_msg.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before,
//					int count) {
//				btn_send.setEnabled(s == null || s.length() <= 0 ? false : true);
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count,
//					int after) {
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//			}
//		});
//		findViewById(R.id.btn_send).setOnClickListener(this);
		list = (ListView) findViewById(android.R.id.list);

		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public MessageListFragment() {
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		EventBus.getDefault().register(this);
	}
	@Override
	public void onDetach() {
		super.onDetach();
		EventBus.getDefault().unregister(this);
	}
	public void onEvent(ReadMsg mReadMsg){
		if (messageType==1) {
			initdata();
		}
	}
	String urls[] = { "GetSystemMessage?userid=%s", "GetMessage?userid=%s",
			"GetReadMessage?userid=%s", "GetSendMessage?userid=%s" };
	private int messageType;
	public static final String key = "messageType";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_messagelist, container,
				false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_talking_btn_mail_p);
		bindViews();
		messageType = Integer.parseInt(getArguments().getString(KEY_USER_ID));
		initdata();
		return rootView;
	}

	private void initdata() {
		String[] from = new String[] { "username", BYQUserInfo.KEY_SHENFEN,
				"msgcount", "stime", "avatar",BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT  };
		int[] to = new int[] { R.id.tv_nick, R.id.age,  R.id.count,R.id.time, R.id.iv_avator, R.id.iv_vip,R.id.imageView1, };
		int res = R.layout.item_list_msg;
		switch (messageType) {
		case 0:
			res = R.layout.item_list_sysmsg;
			from = new String[] { "subject", "sendtime" };
			to = new int[] { R.id.tv_nick, R.id.time };
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;

		default:
			break;
		}

		initAbsListView(getListView(), API.URL + urls[messageType],
				new Object[] { UserManger.getInstance().userid }, res,
				from, to);

	}

	@Override
	protected String preFormatJsonData(String t) {
		return BYQUserInfo.preFormatShenFen(t);
	}

	@Override
	public Object getOnItemListener(String[] from, String t)
			throws JSONException {
		return new OnMsgItemClickListener(t,messageType);
	}

	@Override
	public void onClick(View v) {
//		Editable input = et_msg.getText();
//		if (TextUtils.isEmpty(input)) {
//			return;
//		}
//		Object[] args = new Object[] { UserManger.getInstance().userid,
//				UserManger.getInstance().userid, input.toString() };
//		BaseControl.get(API.URL
//				+ "SendMessage?fuserid=%s&tuserid=%s&content=%s",
//				new AjaxCallBackProxy() {
//					@Override
//					public void onSuccess(String t) {
//						super.onSuccess(t);
//						try {
//							if ("0".equals(new JSONObject(t).getString("msg"))) {
//								Tip.show(ERR);
//								return;
//							}
//							if ("1".equals(new JSONObject(t).getString("msg"))) {
//								Tip.show(OK);
//								return;
//							}
//						} catch (Exception e) {
//							e.printStackTrace();
//							onFailure(e, 0, e.getMessage());
//						}
//					}
//
//					@Override
//					public void onFailure(Throwable t, int errorNo,
//							String strMsg) {
//						super.onFailure(t, errorNo, strMsg);
//						Tip.show(ERR);
//					}
//				}, args);
	}
}